﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Reviewer.Entities;
using Reviewer.Entities.Services;
using Reviewer.Business;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Globalization;

public partial class public_html_NewSiteTest_Releaseliste : System.Web.UI.Page
{
    public string SpilID = "";
    public string Format = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int reviewId = -1;
            if (Request["ReviewId"] != null)
            {
                reviewId = int.Parse(Request["ReviewId"]);
            }

            using (IReviewService reviewService = ServiceFactory.ReviewService)
            using (IGameService gameService = ServiceFactory.GameService)
            {
                #region Old method
                if (reviewId < 0)
                {
                    if (Request["Id"] != null)
                    {
                        reviewId = int.Parse(Request["Id"]);
                    }
                }
                #endregion

                Review review = reviewService.Find(reviewId);
                if (review != null && review.GameId > 0)
                {
                    Game reviewedGame = gameService.GetBy(review.GameId);

                    //Databind review
                    lblHeader.Text = review.Header;
                    lblDato.Text = review.LastModifiedDate.ToLongDateString() + " " + review.LastModifiedDate.ToString("HH:mm");
                    uiDtReviewed.Attributes["content"] = review.LastModifiedDate.ToString("yyyy-MM-dd HH:mm:ss");

                    imgKarakter.ImageUrl = ResolveUrl("~/grafik/karakter" + review.Rating.ToString() + ".png");
                    //uiRating.Attributes["content"] = ((5D / 6D) * (double)review.Rating).ToString("#.0").Replace(",", ".");
                    uiRating.Attributes["content"] = review.Rating.ToString();

                    string[] reviewTextSections = Regex.Split(Regex.Replace(review.Text, Environment.NewLine, "<br />"), "<br /><br />");
                    StringBuilder reviewText = new StringBuilder();

                    GameScreenshot[] gameScreenshots = gameService.GetAssociatedScreenshots(reviewedGame.GameId).Where(t => t.Format.Equals(reviewedGame.Format)).ToArray();

                    int gameScreenShotIndex = 0;
                    foreach (string reviewTextSection in reviewTextSections)
                    {
                        reviewText.Append(reviewTextSection);
                        if (gameScreenShotIndex < gameScreenshots.Length)
                        {
                            reviewText.AppendFormat("<br /><center><a href=\"{0}\" class=\"lightbox\" rel=\"anmeldelse\" title=\"{2}\"><img style=\"border:1px solid black; margin:6px;\" width=\"370\" src=\"{1}\" /></center></a><br />"
                                , UrlUtillity.GetGamescreenshotImageUri(gameScreenshots[gameScreenShotIndex])
                                //, UrlUtillity.GetGameScreenshotUri(gameScreenshots[gameScreenShotIndex])
                                , UrlUtillity.GetGamescreenshotImageUri(gameScreenshots[gameScreenShotIndex])
                                , reviewedGame.Title + " - " + review.Header
                                
                                );
                        }
                        else
                        {
                            reviewText.Append("<br /><br />");
                        }
                        gameScreenShotIndex++;
                    }

                    lblTekst.Text = reviewText.ToString();

                    using (IDistributorService distributorService = ServiceFactory.DistributorService)
                    {
                        Distributor distributor = distributorService.GetAll().FirstOrDefault(t => t.Id.Equals(review.Distributor));
                        lblDis.Text = (distributor == null) ? "Ukendt distributør" : distributor.Name;
                    }

                    //Databind game info
                    lblTitel.Text = reviewedGame.Title;
                    lblInfoSpil.Text = "<a href=\"" + ResolveUrl(UrlUtillity.GetGameUri(reviewedGame)) + "\">Klik her for mere info</a>";
                    lblMulti.Text = HelperUtilities.GetMultiplayerInfoName(reviewedGame.MultiplayerInfo);
                    lblUdgiver.Text = reviewedGame.Publisher;
                    lblUdvikler.Text = reviewedGame.Developer;
                    imgCover.Width = 200;
                    imgCover.ImageUrl = UrlUtillity.GetGameCoverUri(reviewedGame);

                    string formatName = "Ukendt format";
                    using (IMetadataService<Format> formatService = ServiceFactory.GetMetadataService<Format>())
                    {
                        Format format = formatService.GetAll().FirstOrDefault(t => t.Id.Equals(reviewedGame.Format));
                        if (format != null)
                        {
                            formatName = format.Name;
                        }

                        lblFormat.Text = formatName;
                    }

                    using (IMetadataService<Genre> genreService = ServiceFactory.GetMetadataService<Genre>())
                    {
                        Genre genre = genreService.GetAll().FirstOrDefault(t => t.Id.Equals(reviewedGame.Genre));
                        lblGenre.Text = (genre == null) ? "Ukendt genre" : genre.Name;
                    }

                    //Databind author info
                    using (IWriterService writerService = ServiceFactory.WriterService)
                    {
                        Writer writer = writerService.Find(review.Author);
                        lblForfatter.Text = (writer == null) ? "Ukendt forfatter" : writer.LogOnName;
                    }

                    HtmlMeta HeaderDescription = new HtmlMeta();
                    HeaderDescription.Name = "description";
                    HeaderDescription.Content = "Anmeldelse af " + reviewedGame.Title + " til " + formatName + " - " + review.Header + "...";
                    this.Header.Controls.Add(HeaderDescription);
                    this.Title = "Anmeldelse af " + reviewedGame.Title + " til " + formatName;

                    //Log visit
                    using (IGameViewService gameViews = ServiceFactory.GameViewService)
                    {
                        gameViews.LogView(reviewedGame.Id, reviewedGame.GameId);
                    }

                    return; //all good
                }
            }
        }
        Response.Redirect(ResolveUrl("~/404.aspx"));
    }
}
